<?php

namespace app\Api\controller;

use think\Loader;
use app\common\controller\Apps as Controller;
use think\Exception;
use app\common\Token;
use think\helper\Str;
use app\user\traits\controllers\TraitUser;

class Index extends Controller
{
	use TraitUser;

	protected $exceptCheckToken = ['user.getToken', 'user.getWxsession','caravan.getInfo','caravan.getCaravanAllTypes', 'phonecode.Send'];

    protected function callAction($controller)
    {
    	$path = explode('/', $this->request->path());
    	if ($m = array_pop($path)) {
            $method = Str::lower($this->request->method()) . Str::studly($m);
    		if (!in_array($this->request->param('action') .'.' .$method, $this->exceptCheckToken)) {
    			$this->tokenUserInfo();
    		}
    		$controller->apps = $this;
    		return $controller->$method($this);
    	}
    	throw new Exception("URL error", 1);
    }

    public function _empty($name)
    {
    	return $this->callAction(Loader::controller($this->request->param('action'), 'event'));
    }

    public function getUuid()
    {
        try {
            $client = new \WebSocketClient('127.0.0.1', 9503);
            $client->connect();
            for ($i=0; $i < 20; $i++) { 
                $client->send("hello swoole");
                $tmp = $client->recv();
                echo $tmp;
                sleep(1);
            };
            return ;
            
        } catch (\Exception $e) {
            return ['code' => 200, 'data' => $e->getMessage()];
        }
    }

    public function getSearch()
    {
        if ($token = $this->request->header('token')) {
            $this->tokenUserInfo();
        }
		$car_area = $this->request->param('area');
		$sdate    = $this->request->param('sdate');
		$edate    = $this->request->param('edate');
        $page     = $this->request->param('page', 0);

    	return ['code' => 200, 'data' => Loader::model('Caravans')->search(['car_area' => $car_area, 'sdate' => $sdate, 'edate' => $edate], $page)];
    }

    public function getCamps()
    {
        return ['code' => 200, 'data' => Loader::model('Camps')->all()];
    }

    public function getSendCode()
    {
        return Loader::model('PhoneCode')->Send($this->request->param('phone'), $this->request->param('uuid'));
    }

    public function getRegister()
    {
        $info['mobile'] = input('get.mobile');
        $phone_code = input('get.code');
        $uuid = input('get.uuid');
        $info['password'] = input('get.pass');
        $info['sign'] = $this->uuid();
        return $user = $this->postRegister($info, $phone_code, $uuid);
    }

    public function postTestlocation()
    {
        $lat = $this->request->param('lat');
        $lng = $this->request->param('lng');
        return db('locations')->insert(['lat' => $lat, 'lng' => $lng]);
    }

    public function getTt()
    {
        return $this->base64EncodeImage($this->request->param('pic'));
    }

    public function getT()
    {
        return ['code' => 200, 'data' => db('locations')->select()];
    }

}
